//
//  LKShareManager.m
//  LLKMutiSDK
//
//  Created by mac on 2021/10/26.
//

#import "LKShareManager.h"


@interface LKShareManager()<UnityFrameworkListener,NativeCallsProtocol>

@property(nonatomic,strong)NSString *_sceneId;

@end


@implementation LKShareManager


+(instancetype)shareManager{
    static LKShareManager *shareManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!shareManager) {
            shareManager = [[LKShareManager alloc] init];
        }
    });
    return shareManager;
}

-(void)quitApp{
    [self AirlookCitySandPause];
}

- (void)showHostMainWindow:(NSString *)color {
    
}


- (void)setSceneId:(NSString *)key{
    self._sceneId = key;
}

- (UIView *)OpenSDK:(int)agrc agrv:(char *[])argv appLaunchOpts:(NSDictionary*)appLaunchOpts {
    //[self changeFrame:true];
    UIView * citySandView;
    if ([self unityIsInitialized]) {
        [UnityFrameworkLoad() pause:false];
        [[self ufw] showUnityWindow];
        [[self ufw] sendMessageToGOWithName:[@"GameMain" UTF8String] functionName:[@"ReEnter" UTF8String] message:[[NSString stringWithFormat:@"%@", self._sceneId] UTF8String]];
        citySandView = [[[self ufw] appController] rootView];
    }else {
        [self setUfw: UnityFrameworkLoad()];
        [[self ufw] setDataBundleId: "com.unity3d.framework"];
        [[self ufw] registerFrameworkListener: self];
        [NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
        [[self ufw] runEmbeddedWithArgc: agrc argv: argv appLaunchOpts: appLaunchOpts];
        [[self ufw] appController].quitHandler = ^(){
            NSLog(@"AppController.quitHandler called");};
        [self.ufw sendMessageToGOWithName:[@"GameMain" UTF8String] functionName:[@"SetUniqSceneKey" UTF8String] message:[[NSString stringWithFormat:@"%@", self._sceneId] UTF8String]];
        citySandView = [[[self ufw] appController] rootView];
        
    }
    
    return citySandView;
}

- (void)AirlookCitySandPause {
    if ([self unityIsInitialized]) {
//        [self changeFrame:false];
        [UnityFrameworkLoad() pause:true];
        [[UIApplication sharedApplication].delegate.window makeKeyAndVisible];
//        [[self ufw] appController].window
        if ([self.delegate respondsToSelector:@selector(AirlookCitySandDidPause)]) {
            [self.delegate AirlookCitySandDidPause];
        }
        
    } else {
        [UnityFrameworkLoad() quitApplication:0];
    }
}

- (void)AirlookCitySandUnload {
    if ([self unityIsInitialized]) {
        [[self ufw] quitApplication:0];
    }
}

- (bool)unityIsInitialized { return [self ufw] && [[self ufw] appController]; }

#pragma mark ---UntiyDelegate---

- (void)unityDidUnload:(NSNotification*)notification {
    if (self.ufw) {
        [[self ufw] unregisterFrameworkListener:self];
        [self setUfw: nil];
        [self showHostMainWindow:@""];
    }
//    [[UIApplication sharedApplication].delegate.window makeKeyAndVisible];
    if ([self.delegate respondsToSelector:@selector(AirlookCitySandDidUnload)]) {
        [self.delegate AirlookCitySandDidUnload];
    }
}

-(void)unityDidQuit:(NSNotification *)notification{
    NSLog(@"unityDidQuit called");
    
    if([self unityIsInitialized]) {
        [[self ufw] unregisterFrameworkListener: self];
//        [[self ufw] quitApplication:1];
        [self setUfw:nil];
    } else {
        [UnityFrameworkLoad() unloadApplication];
    }
//    [[self ufw] unregisterFrameworkListener: self];
//    [self setUfw: nil];
//    [UnityFrameworkLoad() quitApplication:0];
//    [self showHostMainWindow:@""];
}

UnityFramework* UnityFrameworkLoad()
{
    NSString* bundlePath = nil;
    bundlePath = [[NSBundle mainBundle] bundlePath];
    bundlePath = [bundlePath stringByAppendingString: @"/Frameworks/UnityFramework.framework"];

    NSBundle* bundle = [NSBundle bundleWithPath: bundlePath];
    if ([bundle isLoaded] == false) [bundle load];

    UnityFramework* ufw = [bundle.principalClass getInstance];
    if (![ufw appController])
    {
        // unity is not initialized
        [ufw setExecuteHeader: &_mh_execute_header];
    }
    return ufw;
}

@end
